var express = require('express');
var router = express.Router();
var { msglistModel } = require('../model/model');

/* GET home page. */
router.get('/msglist', async (req, res) => {
  let data = await msglistModel.find()
  res.send({
    code: 200,
    data
  })
})

router.post('/saveMsg', async (req, res) => {
  let data = req.body

  let D = await msglistModel.find()

  if (D.length >= 50) {
    await msglistModel.deleteOne({ _id: D[0]._id })
  }

  console.log(data);
  msglistModel.create(data)
  res.send({
    code: 200,
    msg: '保存成功'
  })
})

router.get('/clearAll',async (req, res) => {
  await msglistModel.deleteMany()
  res.send({
    code: 200,
    msg: '删除成功'
  })
})

module.exports = router;